//
//  InfoHeaderView.m
//  小团购
//
//  Created by 樊荣海 on 15/2/6.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "InfoHeaderView.h"

@implementation InfoHeaderView {
    
    CGFloat _h;
}

//- (void)awakeFromNib {
//
//    _h = self.frame.size.height;
//}

+ (id)infoHeaderView {
    
    return [[NSBundle mainBundle] loadNibNamed:@"InfoHeaderView" owner:nil options:nil][0];
}

- (void)setDealModel:(DealModel *)dealModel {
    
    NSLog(@"setDealModel");
    
    _dealModel = dealModel;
    
    if (_dealModel.restrictions) { // 有约束（完整的数据）
        // 1.设置是否支持退款
        _suishi.enabled = _dealModel.restrictions.is_refundable;
        _guoqi.enabled = _suishi.enabled;
    } else {
        
        [_image sd_setImageWithURL:[NSURL URLWithString:_dealModel.image_url] placeholderImage:[UIImage imageNamed:@"placeholder_deal.png"]];
        
        // 3.设置剩余时间
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy-MM-dd"; // 2013-11-17
        // 2013-11-17
        NSDate *deadDate = [fmt dateFromString:_dealModel.purchase_deadline];
        
        //[重要]过期时间+1天
        deadDate = [deadDate dateByAddingTimeInterval:24 * 3600];
        
        NSDateComponents *cmp = [NSDate cmpsFromData:[NSDate date] toDate:deadDate];
        
        NSString *timeStr = [NSString stringWithFormat:@"%d 天 %d 小时 %d 分钟", cmp.day, cmp.hour, cmp.minute];
        
        [_time setTitle:timeStr forState:UIControlStateNormal];
        
    }
    
    // 4.购买人数
    NSString *pc = [NSString stringWithFormat:@"%d 人已购买", _dealModel.purchase_count];
    [_people setTitle:pc forState:UIControlStateNormal];
    
    //5.文字
    _tip.text = _dealModel.restrictions.special_tips;
    
    CGFloat newHeight = [LabelTool sizeOfStr:_tip.text andFont:_tip.font andMaxSize:CGSizeMake(_tip.frame.size.width, 100000) andLineBreakMode:_tip.lineBreakMode].height + 10;
    
    //与之前相差多少
    CGFloat daltaH = newHeight-_tip.frame.size.height;
    
    //改变 label
    CGRect rect = _tip.frame;
    rect.size.height = newHeight;
    _tip.frame = rect;
    
    //改变 整体
    CGRect rect2 = self.frame;
    rect2.size.height += daltaH;
    self.frame = rect2;

}


@end
